//
//  TRNavigationBarDefine.h
//  TRNavigationBar
//
//  Created by QuintGao on 2019/10/27.
//  Copyright © 2019 QuintGao. All rights reserved.
//

#ifndef TRNavigationBarDefine_h
#define TRNavigationBarDefine_h

#import <objc/runtime.h>

// 屏幕相关
#define TR_SCREEN_WIDTH         [UIScreen mainScreen].bounds.size.width
#define TR_SCREEN_HEIGHT        [UIScreen mainScreen].bounds.size.height

// 判断是否是刘海屏
#define TR_NOTCHED_SCREEN      ([UIScreen instancesRespondToSelector:@selector(currentMode)] ?\
(\
CGSizeEqualToSize(CGSizeMake(375, 812),[UIScreen mainScreen].bounds.size)\
||\
CGSizeEqualToSize(CGSizeMake(812, 375),[UIScreen mainScreen].bounds.size)\
||\
CGSizeEqualToSize(CGSizeMake(414, 896),[UIScreen mainScreen].bounds.size)\
||\
CGSizeEqualToSize(CGSizeMake(896, 414),[UIScreen mainScreen].bounds.size))\
:\
NO)

// 判断是否是iPad
#define TR_IS_iPad                  UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad

// 顶部安全区域高度
#define TR_SAFEAREA_TOP             (TR_NOTCHED_SCREEN ? 24.0f : 0.0f)
// 底部安全区域高度
#define TR_SAFEAREA_BTM             (TR_NOTCHED_SCREEN ? 34.0f : 0.0f)
// 状态栏高度
#define TR_STATUSBAR_HEIGHT         (TR_NOTCHED_SCREEN ? 44.0f : 20.0f)
// 导航栏高度
#define TR_NAVBAR_HEIGHT            44.0f
// 状态栏+导航栏高度
#define TR_STATUSBAR_NAVBAR_HEIGHT  (TR_STATUSBAR_HEIGHT + TR_NAVBAR_HEIGHT)
// tabbar高度
#define TR_TABBAR_HEIGHT            (TR_NOTCHED_SCREEN ? 83.0f : 49.0f)

// 导航栏间距，用于不同控制器之间的间距
static const CGFloat TRNavigationBarItemSpace = -1;

// 返回按钮样式
typedef NS_ENUM(NSUInteger, TRNavigationBarBackStyle) {
    TRNavigationBarBackStyleNone,   // 无返回按钮
    TRNavigationBarBackStyleBlack,  // 黑色返回按钮
    TRNavigationBarBackStyleWhite   // 白色返回按钮
};

// 使用static inline创建静态内联函数，方便调用，新方法默认自带前缀gk_
static inline void tr_swizzled_instanceMethod(Class oldClass, NSString *oldSelector, Class newClass) {
    NSString *newSelector = [NSString stringWithFormat:@"tr_%@", oldSelector];
    
    SEL originalSelector = NSSelectorFromString(oldSelector);
    SEL swizzledSelector = NSSelectorFromString(newSelector);
    
    Method originalMethod = class_getInstanceMethod(oldClass, NSSelectorFromString(oldSelector));
    Method swizzledMethod = class_getInstanceMethod(newClass, NSSelectorFromString(newSelector));
    
    BOOL isAdd = class_addMethod(oldClass, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
    if (isAdd) {
        class_replaceMethod(newClass, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    }else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}

#endif /* TRNavigationBarDefine_h */
